package com.example.service.impl;

import com.example.entity.*;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Page.PageResult;
import com.example.mapper.HouseholdMapper;
import com.example.result.CommonResult;
import com.example.service.HouseholdService;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @project: smart-property
 * @description: 住户管理 service 实现层
 * @author: cjq
 * @create: 2022-11-21 13:54
 **/
@Service
public class HouseholdServiceImpl implements HouseholdService {
    @Autowired
    HouseholdMapper householdMapper;

    @Autowired
    RedisUtil redisUtil;

    Integer cid;

    /**
     * 房屋联级查询
     * @param cid
     * @return
     */
    @Override
    public List<BuildingModel> listHouse(Integer cid) {
        List<BuildingModel> building = householdMapper.listBuilding(cid);
        List<UnitModel> unit = householdMapper.listUnit();
        List<HouseModel> floor = householdMapper.listFloor();

        // 组装单元信息
        String unitKey = "unitKey_";
        Map<String,List<UnitModel>> getUnitMap = (Map<String, List<UnitModel>>) redisUtil.get("getUnitMap");
        if (getUnitMap == null){
            getUnitMap = getUnitMap(unit,unitKey);
        }
        // 将单元放到楼栋里
        for (int i = 0;i<building.size();i++){
            BuildingModel buildingModel = building.get(i);
            List<UnitModel> unitModels = getUnitMap.get(unitKey + buildingModel.getId());
            buildingModel.setChildren(unitModels);
        }
        // 组装房屋信息
        String houseKey = "houseKey_";
        Map<String, List<HouseModel>> getHouseMap = (Map<String, List<HouseModel>>) redisUtil.get("getHouseMap");
        if (getHouseMap == null){
            getHouseMap = getHouseMap(floor,houseKey);
        }
        // 将房屋放到单元里
        for (int i = 0; i<unit.size();i++){
            UnitModel unitModel = unit.get(i);
            List<HouseModel> houseModels = getHouseMap.get(houseKey + unitModel.getId());
            unitModel.setChildren(houseModels);
        }
        return building;
    }

    /**
     * 住户编号查重
     * @param hNumber
     * @param villageId
     * @return
     */
    @Override
    public CommonResult listHnumber(String hNumber, Integer villageId) {
        Household household = householdMapper.listHnumber(hNumber, villageId);
        if (!StringUtils.isEmpty(household)){
            return CommonResult.ok();
        }
        return CommonResult.fail(household);
    }

    /**
     * 添加/修改住户信息
     * @param household
     * @return
     */
    @Override
    public int saveHouseHold(Household household) {
        int i = 0;
        if (household.getId() == null){
            household.setSourceOfHouseholds("后台注册");
            household.setDelFlag(0);
            household.setStart(0);
            if (household.getRelation().equals("本人")){
                household.setHouseholdType("业主");
            }else {
                household.setHouseholdType(household.getRelation());
            }
            household.setHouse(household.getHouse().replaceAll(",",""));
            household.setAuditTime(new Date());
            household.setCreatTime(new Date());
            household.setHouseholdRead("0");
            String houseHold = "houseHold_"+this.cid;
            redisUtil.del(houseHold);
            i = householdMapper.addHouseHold(household);
        }else {
            household.setSourceOfHouseholds("后台注册");
            if (household.getRelation().equals("本人")){
                household.setHouseholdType("业主");
            }else {
                household.setHouseholdType(household.getRelation());
            }
            household.setHouse(household.getHouse().replaceAll(",",""));
            household.setDelFlag(0);
            String houseHold = "houseHold_"+this.cid;
            redisUtil.del(houseHold);
            i = householdMapper.updateHouseHold(household);
        }
        return i;
    }

    /**
     * 住户列表展示
     * @param householdType
     * @param start
     * @param type
     * @param input
     * @param house
     * @param villageId
     * @param startTime
     * @param endTime
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult queryHouseHold(String householdType, Integer start,String type,String input,String startTime, String endTime,String house, Integer villageId ,PageRequest pageRequest) {
        this.cid = villageId;
        String houseHold = "houseHold_"+this.cid;
        List<Household> list = (List<Household>) redisUtil.get(houseHold);
        if (householdType == "" && start == null && type == "" && input == "" && startTime == null && endTime == null && house == ""){
            if (list == null){
                list = householdMapper.queryHouseHold(householdType,start,type,input,startTime,endTime,house,villageId);
                redisUtil.set(houseHold,list);
            }
        }else {
            return MyPageHelper.getPageResult(householdMapper.queryHouseHold(householdType,start,type,input,startTime,endTime,house,villageId));
        }
        return MyPageHelper.getPageResult(list);
    }

    /**
     * 住户审核
     * @param id
     * @param examineMessage
     * @param start
     * @return
     */
    @Override
    public int examineStart(Integer id, String examineMessage, Integer start) {
        String houseHold = "houseHold_"+this.cid;
        redisUtil.del(houseHold);
        return householdMapper.examineStart(id,examineMessage,start);
    }

    /**
     * 删除住户信息
     * @param id
     * @return
     */
    @Override
    public int delHouseHold(Integer id) {
        String houseHold = "houseHold_"+this.cid;
        redisUtil.del(houseHold);
        return householdMapper.delHouseHold(id);
    }

    /**
     * 批量删除住户信息
     * @param ids
     * @return
     */
    @Override
    public int batchDel(Integer[] ids) {
        String houseHold = "houseHold_"+this.cid;
        redisUtil.del(houseHold);
        return householdMapper.batchDel(ids);
    }

    /**
     * 住户统计
     * @param villageId
     * @return
     */
    @Override
    public Count countHouseHoid(Integer villageId) {
        System.out.println(householdMapper.countHouseHoid(villageId));
        return householdMapper.countHouseHoid(villageId);
    }

    // 存储单元信息
    public Map<String,List<UnitModel>> getUnitMap(List<UnitModel> unitModelList,String unitKey){
        Map<String,List<UnitModel>> map = new HashMap<>();
        for (int i = 0;i<unitModelList.size();i++){
            UnitModel unitModel = unitModelList.get(i);
            List<UnitModel> houseModels = map.get(unitKey + unitModel.getBid());
            if (houseModels == null){
                houseModels = new ArrayList<>();
                houseModels.add(unitModel);
                map.put(unitKey + unitModel.getBid(),houseModels);
            }else {
                houseModels.add(unitModel);
            }
        }
        return map;
    }

    // 存储房屋信息
    public Map<String,List<HouseModel>> getHouseMap(List<HouseModel> houseModelList,String houseKey){
        Map<String,List<HouseModel>> map = new HashMap<>();
        for (int i = 0;i<houseModelList.size();i++){
            HouseModel houseModel = houseModelList.get(i);
            List<HouseModel> houseModels = map.get(houseKey + houseModel.getUid());
            if (houseModels == null){
                houseModels = new ArrayList<>();
                houseModels.add(houseModel);
                map.put(houseKey + houseModel.getUid(),houseModels);
            }else {
                houseModels.add(houseModel);
            }
        }
        return map;
    }
}
